package leetcode;

import common.ListNode;
import common.Util;

/**
 * 旋转链表  
 */
public class LeetCode61 {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null || head.next == null || k == 0) return head;
        int length = 1;
        ListNode cur = head;
        while (cur != null && cur.next != null) {
            cur = cur.next;
            length++;
        }
        cur.next = head;
        k = length - k % length;
        for (int i = 0; i < k; i++) {
            head = head.next;
            cur = cur.next;
        }
        cur.next = null;
        return head;
    }
    public static void main(String[] args) {
        LeetCode61 leetCode61 = new LeetCode61();
        System.out.println(Util.formatListNode(leetCode61.rotateRight(common.Util.createListNode(5), 2)));;
    }
}